<?php
class MyController extends CController
{
	public $js = array();
	public function init()
	{
		parent::init();
	}
	public static function get_url($url,$refer='')
	{
		//启动一个CURL会话
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_REFERER, $refer);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

		// 执行操作
		$result = curl_exec($ch);
		$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);

		// 关闭CURL会话
		curl_close($ch);
		return array('code'=>$httpcode,'content'=>$result);
	}
	
	/**
	 * 得到用户的IP
	 */
	public static function getUserHostAddress()
	{
		switch(true)
		{
			case ($ip=getenv("HTTP_X_FORWARDED_FOR")):
				break;
			case ($ip=getenv("HTTP_CLIENT_IP")):
				break;
			default:
				$ip=getenv("REMOTE_ADDR") ? getenv("REMOTE_ADDR") : '127.0.0.1';
		}
		if (strpos($ip, ', ') > 0)
		{
			$ips = explode(', ', $ip);
			$ip = $ips[0];
		}
		return $ip;
	}
	
	public function json_return($status,$msg,$redirect='',$extra_data=null)
	{
		$return = array('status'=>$status,'msg'=>$msg,'redirect'=>$redirect,'extra'=>$extra_data);
		die(json_encode($return));
	}
}
function dollar_rate()
{
	$return = Yii::app()->cache->get('dollarforex');
	if($return === false)
	{
		$readdat = file_get_contents('http://hq.sinajs.cn/list=USDCNY');
		$return  = substr($readdat, 28,6);
		Yii::app()->cache->set('dollarforex',$return,3600);
	}
	return $return;
}

function dollar_baidu()
{
	$return = Yii::app()->cache->get('dollarbaidu');
	if($return === false)
	{
		$return = 6.2305;
	}
	return $return;
}